package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.DefaultListModel;
import javax.swing.JLayeredPane;

import model.Layer;

public class KoalaLayeredPane extends JLayeredPane {
	private DefaultListModel m_layerList;
	private String m_documentName;
	private Layer m_selectedLayer;
	private int m_selectedLayerIndex;


	public KoalaLayeredPane(int width, int height, String title, Color color,
			int alpha) {
		super();
		m_documentName = title;
		setPreferredSize(new Dimension(width, height));
		m_layerList = new DefaultListModel();
		addLayer(new Layer(width, height, color, "background", alpha));
	}

	public String getDocumentName() {
		return m_documentName;
	}

	public void setDocumentName(String documentName) {
		this.m_documentName = documentName;
	}

	public void addLayer(Layer newLayer) {
		setSelectedLayer(newLayer);
		m_layerList.insertElementAt(newLayer, 0);
		updatePositions();
	}

	public DefaultListModel getList() {
		return m_layerList;
	}

	public void updatePositions() {
		removeAll();
		for (int i = 0; i < m_layerList.getSize(); i++) {
			add((Layer) m_layerList.getElementAt(i), -1);

		}
		repaint();
	}

	public void removeLayer(Layer layer) {
		m_layerList.removeElement(layer);
		updatePositions();
		if (m_layerList.size() != 0)
		    setSelectedLayer((Layer) m_layerList.getElementAt(0));
	}

	public Layer getSelectedLayer() {
		return m_selectedLayer;
	}

	public void setSelectedLayer(Layer layer) {
		m_selectedLayer = layer;
	}

	public void reverseLayers(int startIndex, int endIndex) {

		Layer startLayer = (Layer) m_layerList.getElementAt(startIndex);
		Layer endLayer = (Layer) m_layerList.getElementAt(endIndex);

		if (startIndex > endIndex) {
			m_layerList.insertElementAt(startLayer, endIndex);
			m_layerList.removeElementAt(startIndex + 1);
			m_layerList.insertElementAt(endLayer, startIndex + 1);
			m_layerList.removeElementAt(endIndex + 1);
		} else {
			m_layerList.insertElementAt(startLayer, endIndex);
			m_layerList.removeElementAt(startIndex);
			m_layerList.insertElementAt(endLayer, startIndex);
			m_layerList.removeElementAt(endIndex + 1);
		}
		updatePositions();
	}

	public BufferedImage mixAll() {

		int widthFinale = MainFrame.getInstance().getDocumentWidth();
		int heightFinale = MainFrame.getInstance().getDocumentHeight();
		BufferedImage finale = new BufferedImage(widthFinale, heightFinale,
				BufferedImage.TYPE_INT_ARGB);

		Graphics g = finale.getGraphics();
		for (int i = m_layerList.getSize() - 1; i >= 0; i--) {
			Layer layer = (Layer) m_layerList.getElementAt(i);
			BufferedImage currentImage = layer.getImage();
			g.drawImage(currentImage, layer.getX(), layer.getY(), null);
		}
		g.dispose();

		return finale;
	}

	public BufferedImage fusionLayers(Layer layerTop, Layer layerBot) {
		Graphics g = layerBot.getImage().getGraphics();
		g.drawImage(layerTop.getImage(), layerTop.getX() - layerBot.getX(),
				layerTop.getY() - layerBot.getY(), null);
		g.dispose();

		return layerBot.getImage();
	}

	public int getSelectedLayerIndex() {
		return m_selectedLayerIndex;
	}
	
	public void setSelectedLayerIndex(int index) {
		m_selectedLayerIndex = index;
	}
}
